package com.yimo.chromedriver;

import lombok.Data;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.awt.OSInfo;

import java.util.concurrent.TimeUnit;

/**
 * create by jsm on 2018/11/7
 */
@Component
@Data
public class Crawler {
    private String url;
    @Value("${chromedriver.driver:/usr/local/bin/chromedriver}")
    private String driverPath;

    public ChromeDriver open() {
        System.setProperty("webdriver.chrome.driver", getDriverPath());//设置浏览器驱动//设置浏览器驱动
        System.setProperty("webdriver.gecko.driver", getDriverPath());
        ChromeOptions options = new ChromeOptions();
        if (OSInfo.getOSType().name().equals(OSInfo.OSType.LINUX.name())) {
            options.addArguments("headless");
            options.addArguments("no-sandbox");
        }
//        options.addArguments("start-fullscreen");
        ChromeDriver chromeDriver = new ChromeDriver(options);//new 一个浏览器
        chromeDriver.get(getUrl());//打开网页
        chromeDriver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
        chromeDriver.manage().timeouts().setScriptTimeout(5,TimeUnit.SECONDS);
        return chromeDriver;
    }
}
